package com.xfg.search.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestControllerAdvice
public class GloableException {

    @ExceptionHandler(Exception.class)
    public String exceptionMsg(Exception e, HttpServletResponse response, HttpServletRequest request){
        //解决跨域问题
        response.setHeader("Access-Control-Allow-Origin","*");
        //取出异常信息
        String errorMsg=e.getMessage();
        if (errorMsg!=null){
            JSONObject jsonObject= new JSONObject();
            jsonObject.put("errorMsg",errorMsg);
            return jsonObject.toJSONString();
        }
        return null;
    }
}
